package com.faselbaum.stalkme.fragments;

import java.util.ArrayList;
import java.util.HashMap;

import com.faselbaum.stalkme.R;
import com.faselbaum.stalkme.activities.DevicesList;
import com.faselbaum.stalkme.activities.MainView;
import com.faselbaum.stalkme.models.BluetoothDeviceInfo;
import com.faselbaum.stalkme.models.ProfileInfo;
import com.faselbaum.stalkme.utils.BluetoothAccess;
import com.faselbaum.stalkme.utils.ProfileInfoArrayAdapter;
import com.faselbaum.stalkme.utils.ProfileInfoBuilder;

import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class ProfilesList extends Fragment
{
	
	private View _thisView = null;
	private ListView _listView = null;
	private SeekBar _seekBar = null;
	
	private int _seekBarProgress = 0;
	
	private ArrayList<ProfileInfo> _profilesList = new ArrayList<ProfileInfo>();
	private ProfileInfoArrayAdapter _listAdapter = null;
	private BluetoothAccess _btAccess = null;
	private Activity _activity = null;
	
	//private int _refreshInterval = 2500;
	private int _scanInterval = 5000;
	private int _genericITL = 12;
	
	//private Handler _refreshHandler = new Handler();
	private Runnable _refreshRun = new Runnable()
	{
		@Override
		public void run()
		{
			updateProfilesList();
			//_refreshHandler.removeCallbacks(_refreshRun);
			//_refreshHandler.postDelayed(_refreshRun,_refreshInterval);
		}
	};
	
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
	{
		_thisView = inflater.inflate(R.layout.layout_profileslist, container, false);
		
		_activity = getActivity();
		_listView = (ListView) _thisView.findViewById(R.id.listView_profiles);
		_seekBar = (SeekBar) _thisView.findViewById(R.id.seekBar_radius);
		
		_listAdapter = new ProfileInfoArrayAdapter(_activity.getApplicationContext(),R.layout.list_item_profile_info ,_profilesList);
		_listView.setAdapter(_listAdapter);
		
		_listView.setOnItemClickListener(new OnItemClickListener()
		{
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,	long arg3)
			{
				ProfileInfo info = (ProfileInfo) arg0.getAdapter().getItem(arg2);
				Toast.makeText(_activity.getApplicationContext(), info.getBluetoothDeviceInfo().getMACAddress(), Toast.LENGTH_SHORT).show();
			}
		});
		
		_seekBarProgress = _seekBar.getProgress();		
		_seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
		{
			
			@Override
			public void onStopTrackingTouch(SeekBar seekBar)
			{
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onStartTrackingTouch(SeekBar seekBar)
			{
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser)
			{
				// TODO Auto-generated method stub
				_seekBarProgress = progress;
				updateProfilesList();
			}
		});
		
		_btAccess = new BluetoothAccess(_activity);
		
		if(!_btAccess.isAdapterEnabled())
		{
			Toast.makeText(_activity.getApplicationContext(), R.string.message_activateBluetooth, Toast.LENGTH_SHORT).show();
			if(!_btAccess.setAdapterEnabled(true))
			{
				Toast.makeText(_activity.getApplicationContext(), R.string.message_bluetoothActivationFailed, Toast.LENGTH_SHORT).show();
			}
		}

		_btAccess.setGenericITL(_genericITL);
		_btAccess.setOnUpdateRunnable(_refreshRun);
		_btAccess.setScanInterval(_scanInterval);
		//startRefreshTimer();
		
		return _thisView;
	};
	
	@Override
	public void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		
		
	}
	
	private void updateProfilesList()
	{
		HashMap<String, BluetoothDeviceInfo> devices = _btAccess.getNearbyDevicesMap();
		_profilesList.clear();
		for(BluetoothDeviceInfo device : devices.values())
		{
			if(device.getRelativeRSSI()*_seekBar.getMax()>=(double)_seekBarProgress) _profilesList.add(ProfileInfoBuilder.getInstance().getInfo(device, _activity));
		}
		_listAdapter.notifyDataSetChanged();
	}
    
    private void cleanUp()
    {
    	//stopRefreshTimer();
    	_btAccess.stopScanning();
    }
    
    @Override
    public void onDestroyView()
    {
    	// TODO Auto-generated method stub
    	super.onDestroyView();
    	cleanUp();
    }
	
}
